package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.CompanyDiscount;
import ex.com.jm.food.service.AdminCompanyDiscountService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class AdminCompanyDiscountAction extends ActionSupport {


	private static final long serialVersionUID = 1L;
	private AdminCompanyDiscountService adminCompanyDiscountService;
	private CompanyDiscount companyDiscount;
	private Integer cdId;
	private String cdName;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private Map<String, String> dateMap;
	public CompanyDiscount getCompanyDiscount() {
		return companyDiscount;
	}
	public void setCompanyDiscount(CompanyDiscount companyDiscount) {
		this.companyDiscount = companyDiscount;
	}
	public Integer getCdId() {
		return cdId;
	}
	public void setCdId(Integer cdId) {
		this.cdId = cdId;
	}
	public String getCdName() {
		return cdName;
	}
	public void setCdName(String cdName) {
		this.cdName = cdName;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public AdminCompanyDiscountService getAdminCompanyDiscountService() {
		return adminCompanyDiscountService;
	}
	public void setAdminCompanyDiscountService(
			AdminCompanyDiscountService adminCompanyDiscountService) {
		this.adminCompanyDiscountService = adminCompanyDiscountService;
	}
	
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 到达列表页面
	 * @return
	 */
	public String comdisList(){
		dateMap=DateUtils.getDate();
		return "comdisList";
	}
	
	public void getComDis(){
		  HttpServletResponse response = ServletActionContext.getResponse();  
		  response.setContentType("text/plain;charset=utf-8");  
		  int offset=(initPage-1)*pageRow;
		  List<CompanyDiscount> cdList=this.getAdminCompanyDiscountService().getCompanyDiscounts(offset, pageRow, cdName);
		  totalCount=this.getAdminCompanyDiscountService().cdCount(cdName);
		  JSONArray jsoncd=JSONArray.fromObject(cdList);
		  JSONObject jo=JsonUtils.buildJson(jsoncd, initPage, pageRow, totalCount);
			PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(jo);
			out.print(jo);
		    out.flush();
		    out.close();
	}
	
	public String editComDis(){
		if(cdId!=null){
			companyDiscount=this.getAdminCompanyDiscountService().editComDis(cdId);
		}
		return "edit";
	}
	
	public String saveComDis(){
		if(companyDiscount.getCdName()==null||companyDiscount.getCdName().equals("")){
			addFieldError("companyDiscount.cdName", "优惠名称不能为空");
		}
		if(companyDiscount.getCdId()!=null&&!companyDiscount.getCdId().equals("")){
			this.getAdminCompanyDiscountService().updateComDis(companyDiscount);
			return "comdisList";
		}
		companyDiscount.setCreated(DateUtils.getCurrentDate());
		this.getAdminCompanyDiscountService().saveComDis(companyDiscount);
		return "comdisList";
	}
	
	public String delComDis(){
		this.getAdminCompanyDiscountService().delComDis(cdId);
		return "comdisList";
	}
}
